package club.sanmubird.travel_culture_wx_demo.demo.mysql.controller;

import club.sanmubird.travel_culture_wx_demo.demo.mysql.util.CheckUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

/**
 * @program: travel_culture_wx_demo
 * @description: wx
 * @author: sam
 * @create: 2018-08-04 18:05
 * @update: 2018-08-04 18:05
 * @update: Sam
 **/
@RestController
@RequestMapping("/wx")
public class WxController {

	@RequestMapping("/handle")
	public void handleWx(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("success");
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
				out.write(echostr);
			} else {
				System.out.println("check Failure");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (Objects.nonNull(out)) {
				out.close();
			}
		}
	}

}
